// pages/film-SBZP-up/film-SBZP-up.js
const app = getApp();
const db = wx.cloud.database().collection("actor")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    skill: '',
    contact: '',
    location: '',
    money:'',
    imgUrl:'',
    era:'',
    language:'',
    link:'',
    autosize: {
      maxHeight: 120,
      minHeight: 20,
    },
  },

  handleUpload() {
    let that = this;
    let timestamp = (new Date()).valueOf();
    wx.chooseImage({
     success: chooseResult => {
      wx.showLoading({
       title: '上传中',
      })
      // 将图片上传至云存储空间
      wx.cloud.uploadFile({
       // 指定上传到的云路径
       cloudPath: 'actor/'+timestamp + '.png',
       // 指定要上传的文件的小程序临时文件路径
       filePath: chooseResult.tempFilePaths[0],
       // 成功回调
       success: res => {
        console.log('上传成功', res)
        wx.hideLoading()
        wx.showToast({
         title: '上传图片成功',
        })
        if (res.fileID) {
         that.setData({
          zhaopian: '图片如下',
          imgUrl: res.fileID
         })
        }
  
       },
      })
     },
    })
    },
    publishRent() {
      const { name, skill, contact, location, money, imgUrl,era,language,link } = this.data;
  
      if (!name ||  !contact ) {
        wx.showToast({
          title: '请填写完整信息',
          icon: 'error'
        });
        return;
      }
  
      db.add({
        data: {
          name: name,
          contact: contact,
          location: location,
          picture_url:imgUrl,
          money:money,
          skill:skill,
          era:era,
          language:language,
          link:link


        }
      }).then(res => {

        console.log('数据添加成功', res);
        this.setData({
          name: '',
          skill: '',
          contact: '',
          location: '',
          money: '',
          imgUrl: '' ,
          era:'',
          language:'',
          link:''
        });
        wx.showToast({
          title: '发布成功',
          icon: 'success'
        });
      }).catch(err => {
        // Error handling
        console.error('数据添加失败', err);
        wx.showToast({
          title: '发布失败，请重试',
          icon: 'none'
        });
      });
    },
    onUnload: function() {
      const pages = getCurrentPages(); 
      if (pages.length >= 2) {
        const prevPage = pages[pages.length - 2]; 
        if (prevPage.getEquipmentData) {
    
          prevPage.getEquipmentData();
        }
      }
    }
  
})